<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-shop">&nbsp;&nbsp;仓库管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;质检管理添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="qualityForm"
            :rules="rules"
            ref="qualityForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="10">
              <el-form-item label="日期:" prop="date">
                <el-date-picker
                    v-model="qualityForm.date"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="产品名称:" prop="name">
                <el-input v-model="qualityForm.name" placeholder="请输入产品名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="批号:" prop="dateId">
                <el-input v-model="qualityForm.dateId" placeholder="输入批号"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="产量:" prop="yield">
                <el-input v-model="qualityForm.yield" placeholder="输入产量"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合格数:" prop="qualifiedNumber">
                <el-input v-model="qualityForm.qualifiedNumber" placeholder="输入合格数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="不合格数:" prop="nonConformitiesNumber">
                <el-input v-model="qualityForm.nonConformitiesNumber" placeholder="输入不合格数"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="不合格率:" prop="failureRate">
                <el-input v-model="qualityForm.failureRate" placeholder="输入不合格率"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="合格与否:" prop="isCheck">
            <el-input v-model="qualityForm.isCheck" placeholder="合格与否"></el-input>
          </el-form-item>
          <el-form-item label="备注:" prop="remarks">
            <el-input v-model="qualityForm.remarks" placeholder="输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('qualityForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('qualityForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      qualityForm: {
        date:'2021-03-12',
        name:'电脑',
        dateId:12234,
        yield:12000,
        qualifiedNumber:11800,
        nonConformitiesNumber: 200,
        isCheck:'',
        remarks:'',
        failureRate:0,
      },
      rules: {
        date:[
          {required:true,message:"请选择日期",trigger:'change'}
        ],
        name: [
          {required: true, message: "请输入产品名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        dateId: [
          {required: true, message: "请输入批号", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        yield: [
          {required: true, message: "请输入产量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        isCheck: [
          {required: true, message: "是否合格", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        remarks: [
          {required: true, message: "请输入备注", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        qualifiedNumber: [
          {required: true, message: "请输入合格数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        nonConformitiesNumber: [
          {required: true, message: "请输入不合格数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        failureRate: [
          {required: true, message: "请计算不合格率", trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/wareHouseQuality/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.qualityForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    compute(){
      if(this.qualityForm.qualifiedNumber) {
        let lowQualify = parseInt(this.qualityForm.yield) - parseInt(this.qualityForm.qualifiedNumber);
        this.$set(this.qualityForm, 'nonConformitiesNumber', lowQualify);

        let rate = 100 * lowQualify / (parseInt(this.qualityForm.yield)) + "";
        let num = rate.substring(0, rate.indexOf('.') + 3);
        this.$set(this.qualityForm,'failureRate',num);
        if(num<5){
          this.$set(this.qualityForm,'isCheck','是');
          this.$set(this.qualityForm,'remarks','经过检验计算,产品合格,其中'+(100-num)+'%的产品达到了合格的要求');
        }else {
          this.$set(this.qualityForm,'isCheck','否');
          this.$set(this.qualityForm,'remarks','经过检验计算,产品不合格,其中只有'+(100-num)+'%的产品达到了合格的要求');
        }
      }
      if (this.qualityForm.nonConformitiesNumber){
        let highQualify = parseInt(this.qualityForm.yield) - parseInt(this.qualityForm.nonConformitiesNumber);
        this.$set(this.qualityForm, 'qualifiedNumber', highQualify);

        let rate =100 * parseInt(this.qualityForm.nonConformitiesNumber) / (parseInt(this.qualityForm.yield))+"";
        let num = rate.substring(0, rate.indexOf('.') + 3);
        this.$set(this.qualityForm,'failureRate',num);
        if(num<5){
          this.$set(this.qualityForm,'isCheck','是');
          this.$set(this.qualityForm,'remarks','经过检验计算,产品合格,其中'+(100-num)+'%的产品达到了合格的要求');
        }else {
          this.$set(this.qualityForm,'isCheck','否');
          this.$set(this.qualityForm,'remarks','经过检验计算,产品不合格,其中只有'+(100-num)+'%的产品达到了合格的要求');
        }
      }
    }
  },
}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>